package com.example.yichuangyun.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.example.yichuangyun.R;
import com.example.yichuangyun.utils.DateUtil;

import java.util.Calendar;

public abstract class TimePicker {

    private TimePickerView pickerView;
    private boolean isDialog;
    private Calendar startDate = Calendar.getInstance();
    private Calendar endDate = Calendar.getInstance();

    public TimePicker(Activity activity) {
        startDate.add(Calendar.YEAR, -100);
        endDate.add(Calendar.YEAR, 10);
        showPickerView(activity);
    }

    public TimePicker(Activity activity, boolean isDialog) {
        this.isDialog = isDialog;
        startDate.add(Calendar.YEAR, -100);
        endDate.add(Calendar.YEAR, 10);
        showPickerView(activity);
    }

    public TimePicker(Activity activity, Calendar startDate, Calendar endDate) {
        this.startDate = startDate;
        this.endDate = endDate;
        showPickerView(activity);
    }

    private void showPickerView(Activity activity) {
        pickerView = new TimePickerBuilder(activity, (selDate, v) -> {
            pickerView.dismiss();
            confirm(DateUtil.dateToString(selDate, "yyyy-MM-dd"));
        }).setLayoutRes(R.layout.pk_date_item, v -> {
                    //自定义布局中的控件初始化及事件处理
                    final TextView tvSubmit = v.findViewById(R.id.tv_finish);
                    final TextView tv_cancel = v.findViewById(R.id.tv_cancel);
                    final TextView tv_title = v.findViewById(R.id.tv_title);
                    tv_title.setVisibility(View.INVISIBLE);
                    tvSubmit.setText("确定");
                    tvSubmit.setOnClickListener(v1 -> pickerView.returnData());
                    tv_cancel.setOnClickListener(v12 -> pickerView.dismiss());
                }).setType(new boolean[]{true, true, true, false, false, false})// 默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确定")//确定按钮文字
                .setContentTextSize(18)//滚轮文字大小
                .setTitleSize(20)//标题文字大小
                .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(false)//是否循环滚动
                .setTitleColor(Color.BLACK)//标题文字颜色
                .setSubmitColor(0xFF426FFF)//确定按钮文字颜色
                .setCancelColor(0xFF666666)//取消按钮文字颜色
                .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                .setBgColor(Color.WHITE)//滚轮背景颜色 Night mode
                .setDate(Calendar.getInstance())// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)
                .setLabel("年", "月", "日", "时", "分", "秒")//默认设置为年月日时分秒
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .isDialog(isDialog)//是否显示为对话框样式
                .build();
        if (isDialog) {
            Dialog mDialog = pickerView.getDialog();
            if (mDialog != null) {
                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) pickerView.getDialogContainerLayout().getLayoutParams();
                params.leftMargin = 0;
                params.rightMargin = 0;
                pickerView.getDialogContainerLayout().setLayoutParams(params);
                Window dialogWindow = mDialog.getWindow();
                if (dialogWindow != null) {
                    dialogWindow.setType(WindowManager.LayoutParams.TYPE_APPLICATION);
                    WindowManager.LayoutParams attributes = dialogWindow.getAttributes();
                    attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
                    attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
                    attributes.windowAnimations = com.bigkoo.pickerview.R.style.picker_view_slide_anim; // 修改动画样式
                    attributes.gravity = Gravity.BOTTOM;
                    dialogWindow.setAttributes(attributes);
                }
            }
        }
    }

    public abstract void confirm(String date);

    public void showPickerView() {
        pickerView.show();
    }
}
